<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace App\CustHub\Mapper;

use App\Common\Constants\StatusConstants;
use App\CustHub\Model\ChApiDocCate;
use Hyperf\Cache\Annotation\Cacheable;
use Hyperf\Database\Model\Builder;
use Mine\Abstracts\AbstractMapper;

/**
 * API文档分类Mapper类
 */
class ChApiDocCateMapper extends AbstractMapper
{
    /**
     * @var ChApiDocCate
     */
    public $model;

    public function assignModel()
    {
        $this->model = ChApiDocCate::class;
    }

    /**
     * 获取前端选择树
     * @return array
     */
    public function getSelectTree(): array
    {
        return $this->model::query()
            ->select(['id', 'parent_id', 'id AS value', 'cate_name AS label'])
            ->get()->toTree();
    }


    /**
     * 查询树名称
     * @param array|null $ids
     * @return array
     */
    public function getTreeName(array $ids = null): array
    {
        return $this->model::withTrashed()->whereIn('id', $ids)->pluck('cate_name')->toArray();
    }

    /**
     * @param int $id
     * @return bool
     */
    public function checkChildrenExists(int $id): bool
    {
        return $this->model::withTrashed()->where('parent_id', $id)->exists();
    }

    /**
     * 获取分类列表
     * @return array
     */
    #[Cacheable(prefix: 'ch_api_doc_cate',value: 'list',ttl: 15 * 60)]
    public function getCateList(): array
    {
        return $this->model::select(['id','parent_id','cate_name'])
            ->where('status',StatusConstants::ENABLE)
            ->orderBy('sort')
            ->get()->toTree();
    }



    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {

        //
        if (isset($params['id']) && filled($params['id'])) {
            $query->where('id', '=', $params['id']);
        }

        // 名称
        if (isset($params['cate_name']) && filled($params['cate_name'])) {
            $query->where('cate_name', 'like', '%'.$params['cate_name'].'%');
        }

        // 父级ID
        if (isset($params['parent_id']) && filled($params['parent_id'])) {
            $query->where('parent_id', '=', $params['parent_id']);
        }

        // 层级
        if (isset($params['level']) && filled($params['level'])) {
            $query->where('level', '=', $params['level']);
        }

        // 状态
        if (isset($params['status']) && filled($params['status'])) {
            $query->where('status', '=', $params['status']);
        }

        // 排序
        if (isset($params['sort']) && filled($params['sort'])) {
            $query->where('sort', '=', $params['sort']);
        }

        // 创建者
        if (isset($params['created_by']) && filled($params['created_by'])) {
            $query->where('created_by', '=', $params['created_by']);
        }

        // 更新者
        if (isset($params['updated_by']) && filled($params['updated_by'])) {
            $query->where('updated_by', '=', $params['updated_by']);
        }

        // 创建时间
        if (isset($params['created_at']) && filled($params['created_at']) && is_array($params['created_at']) && count($params['created_at']) == 2) {
            $query->whereBetween(
                'created_at',
                [ $params['created_at'][0], $params['created_at'][1] ]
            );
        }

        // 更新时间
        if (isset($params['updated_at']) && filled($params['updated_at']) && is_array($params['updated_at']) && count($params['updated_at']) == 2) {
            $query->whereBetween(
                'updated_at',
                [ $params['updated_at'][0], $params['updated_at'][1] ]
            );
        }

        return $query;
    }
}